class Solution {
public:
    /**
     * @param people: The i-th person has weight people[i].
     * @param limit: Each boat can carry a maximum weight of limit.
     * @return: Return the minimum number of boats to carry every given person. 
     */
    int numRescueBoats(vector<int> &people, int limit) {
        // Write your code here.
        int boats = 0;
        int left = 0;
        int right = people.size() - 1;
        
        sort(people.begin(), people.end());
        
        while (left <= right) {
           
            if ((people[left] + people[right]) <= limit) {
                left++;
            }
            
            right--;
            boats++;
        }
        
        return boats;
    }
};
